using System;
using System.Configuration;

namespace ECCIT.Data
{
	public class DBConfigManager
	{
        public static readonly string ConnectionString = ConfigurationSettings.AppSettings["CONNECTIONSTRING"].ToString();
        public static readonly DataBaseType DataBaseType = GetDataBaseType();
        public static readonly int CommandTimeout = GetCommandTimeout();
        public static readonly BaseDBHelper ConcreteDBHelper = GetConcreteDBHelper();
        internal static readonly BaseDBHelper.BaseDBFunction ConcreteDBFunction = GetConcreteDBFunction();

		private static DataBaseType GetDataBaseType()
		{
			string sDbType = ConfigurationSettings.AppSettings["DATABASETYPE"].ToString();
			DataBaseType dbtReturn = (DataBaseType)Enum.Parse(typeof(DataBaseType), sDbType);
			return dbtReturn;
		}

        private static int GetCommandTimeout()
        {
            int timeout = 30;
            if (ConfigurationSettings.AppSettings["COMMANDTIMEOUT"] != null)
                timeout = int.Parse(ConfigurationSettings.AppSettings["COMMANDTIMEOUT"].ToString());

            return timeout;
        }

        private static BaseDBHelper GetConcreteDBHelper()
        {
            return DBHelperFactory.CreateDBHelper(DataBaseType);
        }

        private static BaseDBHelper.BaseDBFunction GetConcreteDBFunction()
        {
            DataBaseType dbType = DataBaseType;
            BaseDBHelper.BaseDBFunction dbFunction = null;
            switch (dbType)
            {
                case DataBaseType.SQLServer:
                    dbFunction = new SqlHelper.DBFunction();
                    break;
                case DataBaseType.Oracle:
                    dbFunction = new OracleHelper.DBFunction();
                    break;
                case DataBaseType.OleDb:
                    dbFunction = new OleDbHelper.DBFunction();
                    break;
            }

            return dbFunction;
        }
	}
}
